var nametip = document.getElementById("nametip");
var nametip1 = document.getElementById("nametip1");
var nametip2 = document.getElementById("nametip2");
var pwdtip = document.getElementById("pwdtip");
var comfirm_pwdtip = document.getElementById("comfirm_pwdtip");
var emailtip = document.getElementById("emailtip");
var phonetip = document.getElementById("phonetip");
var emailtip1 = document.getElementById("emailtip1");
var phonetip1 = document.getElementById("phonetip1");
var codetip = document.getElementById("codetip");
var checktip = document.getElementById("checktip");
var name_1 = document.getElementById("name");
var pwd = document.getElementById("pwd");
var comfirm_pwd = document.getElementById("comfirm_pwd");
var email = document.getElementById("email");
var phone = document.getElementById("phone");
var code = document.getElementById("code");
var agree = document.getElementById("agree");
var vertificate_img = document.getElementsByClassName("vertification_code")[0].getElementsByTagName("img")[0];
var change_img = document.getElementsByClassName("change_img")[0].getElementsByTagName("a")[0];

vertificate_img.onclick = function() {
	var randomNum = Math.ceil(Math.random() * 4);
	vertificate_img.setAttribute("src", "img/reg_code_0" + randomNum + ".jpg");
}

change_img.onclick = function() {
	var randomNum = Math.ceil(Math.random() * 4);
	vertificate_img.setAttribute("src", "img/reg_code_0" + randomNum + ".jpg");
}

var reg_name = /^([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|[a-zA-Z0-9]){6,16}$/;
var reg_pwd = /^[a-zA-Z0-9_]{1}\w{5,17}$/;
var reg_email = /^[a-zA-Z\d_]+(\.[a-zA-Z\d_]+)*@([\da-zA-Z](-[\da-zA-Z])?)+(\.{1,2}[a-zA-Z]+)+$/;
var reg_phone = /^1[34578]\d{9}$/;
var vertificate = [{
	src: "img/reg_code_01.jpg",
	code: "dsef"
}, {
	src: "img/reg_code_02.jpg",
	code: "dshf"
}, {
	src: "img/reg_code_03.jpg",
	code: "dshy"
}, {
	src: "img/reg_code_04.jpg",
	code: "qspi"
}]
var vertificate_img = document.getElementsByClassName("vertification_code")[0].getElementsByTagName("img")[0];

name_1.onblur = function(){
	var name_value = name_1.value;
	if (name_value.indexOf(" ") >=0) {
		nametip2.style.display = "block";
		nametip.style.display = "none";
		name_value = name_value.replace(/\s/g, "");
		return false;
	}else{
		nametip2.style.display = "none";
	}

	if (name_value.trim() == "" || !reg_name.test(name_value)) {
		nametip.style.display = "block";
		return false;
	} else {
		nametip.style.display = "none";
		}
}

pwd.onblur = function(){
	var pwd_value = pwd.value;
	if (pwd_value.trim() == "" || !reg_pwd.test(pwd_value)) {
		pwdtip.style.display = "block";
		return false;
	}else {
		pwdtip.style.display = "none";
	}

}

comfirm_pwd.onblur = function(){
	var comfirm_value = comfirm_pwd.value;
	if (comfirm_value.trim() == "" || comfirm_value != pwd.value) {
		comfirm_pwdtip.style.display = "block";
		return false;
	}else {
		comfirm_pwdtip.style.display = "none";
	}
}

email.onblur = function(){
	var email_value = email.value;
	if (email_value.trim() == "" || !reg_email.test(email_value)) {
		emailtip.style.display = "block"; 
		return false;
	}else {
		emailtip.style.display = "none";
	}
}

phone.onblur = function(){
	var phone_value = phone.value;
	if (phone_value.trim() == "" || !reg_phone.test(phone_value)) {
		phonetip.style.display = "block";
		return false;
	} else {
		phonetip.style.display = "none";
	}
}

code.onblur =function(){
	var code_value = code.value;
	if (code_value.trim() == "" || code_value.length != 4) {
		codetip.style.display = "block";
		return false;
	} else {
		for (var i = 0; i < vertificate.length; i++) {
			if (vertificate[i].src == vertificate_img.getAttribute("src")) {
				if (code_value.toLowerCase() != vertificate[i].code) {
					codetip.style.display = "block";
					return false;
				}
			}
		}

	}
	codetip.style.display = "none";
}


function subInput() {
	var name_value = name_1.value;
	var pwd_value = pwd.value;
	var comfirm_value = comfirm_pwd.value;
	var email_value = email.value;
	var phone_value = phone.value;
	var code_value = code.value;
	if (name_value.indexOf(" ") >=0) {
		nametip2.style.display = "block";
		nametip.style.display = "none";
   		name = name.replace(/\s/g, "");
   		return false;
	}else{
		nametip2.style.display = "none";
	}
	
	if (name_value.trim() == "" || !reg_name.test(name_value)) {
		nametip.style.display = "block";
		return false;
	} else {
		nametip.style.display = "none";
	}
	
	if (pwd_value.trim() == "" || !reg_pwd.test(pwd_value)) {
		pwdtip.style.display = "block";
		return false;
	}else {
		pwdtip.style.display = "none";
	}
	
	if (comfirm_value.trim() == "" || comfirm_value != pwd_value) {
		comfirm_pwdtip.style.display = "block";
		return false;
	}else {
		comfirm_pwdtip.style.display = "none";
	}
	
	if (email_value.trim() == "" || !reg_email.test(email_value)) {
		emailtip.style.display = "block"; 
		return false;
	}else {
		emailtip.style.display = "none";
	}
	
	if (phone_value.trim() == "" || !reg_phone.test(phone_value)) {
		phonetip.style.display = "block";
		return false;
	} else {
		phonetip.style.display = "none";
	}
	
	if (code_value.trim() == "" || code_value.length != 4) {
		codetip.style.display = "block";
		return false;
	} else {
		for (var i = 0; i < vertificate.length; i++) {
			if (vertificate[i].src == vertificate_img.getAttribute("src")) {
				if (code_value.toLowerCase() != vertificate[i].code) {
					codetip.style.display = "block";
					return false;
				}
			}
		}

	}
	codetip.style.display = "none";
	
	if (!agree.checked) {
		checktip.style.display = "block";
		return false;
	}else{
		checktip.style.display = "none";
	}

	var userInfo = {
		userName: name_value,
		password: pwd_value,
		email: email_value.toLowerCase(),
		phone: phone_value
	}

	setUserStorage(userInfo);

}

function setUserStorage(obj) {
	var userData = getUserData("userInfos");
	if (!userData) {
		var userInfo = [
			obj
		];
		localStorage.setItem("userInfos", JSON.stringify(userInfo));
		window.location.href="login.html"
	} else {
		var data = JSON.parse(userData);
		var bool = true;
		for (var i = 0; i < data.length; i++) {
			if (data[i].userName == obj.userName) {
				nametip1.style.display = "block";
				bool = false;
				break;
			}else{
				nametip1.style.display = "none";
			}
			if (data[i].email == obj.email) {
				emailtip1.style.display = "block";
				bool = false;
				break;
			}else{
				emailtip1.style.display = "none";
			}
			if (data[i].phone == obj.phone) {
				phonetip1.style.display = "block";
				bool = false;
				break;
			}else{
				phonetip1.style.display = "none";
			}
		}
		if (bool) {
			data.push(obj);
			localStorage.setItem("userInfos", JSON.stringify(data));
			window.location.href = "login.html";
		}
	}
}

function getUserData(userKey) {
	return localStorage.getItem(userKey);
}